/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author thynk
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

public class ProducePanel extends JPanel {
    JLabel nameLabel;
    JComboBox nameField;
    JLabel quantityLabel;
    JTextField quantityField;
    private int productIndex;

    public ProducePanel() {
        nameLabel = new JLabel("Name");
        final String [] arrayOfProducts = (String []) getProductName().toArray (new String[ getProductName().size()]);
        nameField = new JComboBox(arrayOfProducts);
        quantityLabel = new JLabel("Quantity");
        quantityField = new JTextField();

        nameField.setMaximumRowCount( SupplyChainManagement.p.size() );
        nameField.addItemListener (new ItemListener()
        {
                public void itemStateChanged( ItemEvent event )
                {
                    // get Product name
                    if ( event.getStateChange() == ItemEvent.SELECTED )
                        productIndex = nameField.getSelectedIndex();
                }
            

        });

        add(nameLabel);
        add(nameField);
        add(quantityLabel);
        add(quantityField);
    }

    public Dimension getPreferredSize() {
        return new Dimension(350, 200);
    }

    public Dimension getMinimumSize() {
        return new Dimension(350, 200);
    }

    public void doLayout() {
        nameLabel.setBounds(10, 10, 60, 30);
        nameField.setBounds(70, 15, 270, 20);
        quantityLabel.setBounds(10, 40, 60, 30);
        quantityField.setBounds(70, 45, 270, 20);
    }

    public void reset() {
        quantityField.setText("");
    }

    private ArrayList<String> getProductName() {
        ArrayList<String> temp = new ArrayList<String>();

        for (int i = 0; i < SupplyChainManagement.p.size(); ++i) {
            temp.add(SupplyChainManagement.p.get(i).getNameProduct());
        }

        return temp;
    }

    public int getProduct() {
        return productIndex;
    }
}
